package treasurer;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKPayroll;
import controllers.BKPayrollSub;
import controllers.Logs;
import controllers.StudentInformation;

public class PayrollSub extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	
	private Model dateRecModel = new Model();
	private Model payeeModel = new Model();
	private Model amountModel = new Model();
	private Model activitiesModel = new Model();
	private DropDownChoice activities;
	
	public PayrollSub() throws IOException, SQLException
	{
		
		List<BKPayroll> list = (List<BKPayroll>)sqlMap.queryForList("BKPayroll.getActivity");
		
		ListView eachActivity = new ListView("eachActivity", list) {
			private static final long serialVersionUID = 1L;
			
			protected void populateItem(ListItem item) {
				final BKPayroll bk = (BKPayroll) item.getModelObject(); 
			    item.add(new DropDownChoice("activity", activitiesModel, Arrays.asList(bk.getActivity())));
				
			}			
		};
		
		TextField dateRec = new TextField("dateRec", dateRecModel);
		dateRec.setRequired(true);
		
		TextField payee = new TextField("payee", payeeModel);
		payee.setRequired(true);
		
		TextField amount = new TextField("amount", amountModel);
		amount.setRequired(true);
		
		Form form = new Form("PayrollSubForm");
		
		form.add(new Button("add"){
		
		private static final long serialVersionUID = 1L;

		public void onSubmit(){
			
			try {
				
			String activity = (String)activitiesModel.getObject();
			String dateRec = (String)dateRecModel.getObject();
			String payee = (String)payeeModel.getObject();
			String amount = (String)amountModel.getObject();
			
			String uploader = ((Sessions)getSession()).getDesc();
			
			Date today = new Date();
			Timestamp timestamp = new Timestamp(today.getTime());
			String ts = timestamp.toString();
			
			BKPayrollSub bkp = new BKPayrollSub();
			
			
			float newAmount = Float.valueOf(amount);
			
			
			bkp.setActivity(activity);
			bkp.setPayee(payee);
			bkp.setAmount(newAmount);
			bkp.setDateRec(dateRec);
			bkp.setUploader(uploader);
			bkp.setDate_created(ts);
			
			sqlMap.insert("BKPayrollSub.insert", bkp);
			
			Logs log = new Logs();
			log.setLog_activity("Payroll Add Payee: " + bkp.getForm_id());
			log.setLog_officer(uploader);
			log.setLog_time(ts);
			
			sqlMap.insert("Logs.insert", log);
			
			PayrollList page = new PayrollList();
			setResponsePage(page);
			error("added");
			
			}
			catch (SQLException e){
				 error("Fill up necessary fields");
				 e.printStackTrace();
			  } catch (IOException e) {
				e.printStackTrace();
			}
		}});
		
				
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("view", PayrollList.class));
		add(new BookmarkablePageLink("payroll", Payroll.class));
		
		
		add(new FeedbackPanel("error"));
		
		form.add(dateRec);
		form.add(payee);
		form.add(amount);
		form.add(eachActivity);
		//form.add(activities);
		add(form);
		
	}

}